#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using starLiGHT;
using starLiGHT.GUI;
using starLiGHT.GUI.Widgets;
#endregion

namespace starLiGHT.GameStudio.Particles
{
    public class ParticleEditor : Microsoft.Xna.Framework.Game
    {
        #region Private Members
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        GuiManagerComponent guiManagerComponent;

        int frameRate = 0;
        int frameCounter = 0;
        TimeSpan elapsedTime = TimeSpan.Zero;

        ToolStripLabel fpsToolStripLabel;
        #endregion

        public ParticleEditor()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            this.Window.AllowUserResizing = true;

            this.Window.Title = "starLiGHT.GameStudio - Particles v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        }

        protected override void Initialize()
        {
            guiManagerComponent = new GuiManagerComponent(this, @"Content/Skins/DarkSkin.xml");
            guiManagerComponent.GuiManager.SpriteCursorEnabled = true;
            this.Components.Add(guiManagerComponent);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            SetupGui();
        }

        private void SetupGui()
        {
            XnaViewportBox xvb = new XnaViewportBox(guiManagerComponent.GuiManager, "XnaViewport01");
            xvb.Size = new Vector2(800, 600);       //TODO: size to big. It's under MenuStrip and StatusStrip
            xvb.BackColor = Color.CornflowerBlue;   //TODO: dummy color ;-)
            guiManagerComponent.GuiManager.Widgets.Add(xvb);

            MenuStrip menuStrip = new MenuStrip(guiManagerComponent.GuiManager, "MainMenu0");
            menuStrip.Location = new Vector2(0f, 0f);
            menuStrip.Size = new Vector2(GraphicsDevice.Viewport.Width, 23f);

            ToolStripMenuItem tsmi = new ToolStripMenuItem();
            tsmi.Text = "File";
            menuStrip.Items.Add(tsmi);

            ToolStripMenuItem exitMenuItem = new ToolStripMenuItem();
            exitMenuItem.Text = "Exit";
            exitMenuItem.Click += new EventHandler(exitMenuItem_Click);
            tsmi.DropDownItems.Add(exitMenuItem);

            tsmi = new ToolStripMenuItem();
            tsmi.Text = "Help";
            menuStrip.Items.Add(tsmi);

            guiManagerComponent.GuiManager.Widgets.Add(menuStrip);

            StatusStrip statusStrip = new StatusStrip(guiManagerComponent.GuiManager, "StatusStrip0");
            statusStrip.Size = new Vector2(GraphicsDevice.Viewport.Width, 23f);
            statusStrip.Location = new Vector2(0f, GraphicsDevice.Viewport.Height - statusStrip.Size.Y);

            fpsToolStripLabel = new ToolStripLabel();
            fpsToolStripLabel.Text = "FPS: n/a";
            statusStrip.Items.Add(fpsToolStripLabel);

            guiManagerComponent.GuiManager.Widgets.Add(statusStrip);

            Window toolsWindow = guiManagerComponent.GuiManager.CreateWindow("Tools");
            toolsWindow.Size = new Vector2(200, 500);
            toolsWindow.Location = new Vector2(GraphicsDevice.Viewport.Width - toolsWindow.Size.X - 10, menuStrip.Size.Y + 20);

            guiManagerComponent.GuiManager.Widgets.Add(toolsWindow);
        }

        void exitMenuItem_Click(object sender, EventArgs e)
        {
            //TODO: security question
            this.Exit();
        }

        protected override void Update(GameTime gameTime)
        {
            #region Measure FPS
            elapsedTime += gameTime.ElapsedGameTime;

            if (elapsedTime >= TimeSpan.FromSeconds(1))
            {
                elapsedTime -= TimeSpan.FromSeconds(1);
                frameRate = frameCounter;
                frameCounter = 0;

                fpsToolStripLabel.Text = string.Format("FPS: {0}", frameRate);
            }
            #endregion

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            frameCounter++;

            GraphicsDevice.Clear(Color.DarkGray);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
